package com.oristand.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.oristand.pojo.Product;
import com.oristand.pojo.vo.ProductVo;
import com.oristand.service.ProductService;
import com.oristand.util.ResultResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;
import java.util.Objects;

/**
 * 商品接口
 *
 * @author shiyjian
 * @version 1.0
 * @date 2021/11/29 14:42
 */
@RestController
@CrossOrigin
public class ProductController {

    private ProductService productService;

    @Autowired
    public void setProductService(ProductService productService) {
        this.productService = productService;
    }

    @RequestMapping(path = "/findProductVoLikeSkuOrName", method = RequestMethod.POST)
    public ResultResponse findProductVoLikeSkuOrName(@RequestParam(value = "keyword") String str) {
        ResultResponse<List<ProductVo>> resultResponse = new ResultResponse<>();
        List<ProductVo> productVoList = productService.findProductVoLikeSkuOrName(str);
        if (productVoList != null) {
            resultResponse.selectSuccess(productVoList);
        } else {
            resultResponse.selectFail(ResultResponse.SELECT_FAIL);
        }
        return resultResponse;
    }

    @RequestMapping(path = "/findProductVoById/{productId}", method = RequestMethod.GET)
    public ResultResponse findProductVoById(@PathVariable("productId") Integer id) {
        ResultResponse<ProductVo> resultResponse = new ResultResponse<>();
        resultResponse.selectSuccess(productService.findProductVoById(id));
        return resultResponse;
    }

    /**
     * 查询所有的商品
     * /getProductList
     *
     * @return resultResponse 泛型是List<Product> 如果查询成功直接返回商品集合,如果返回失败,则添加错误信息
     * @see ResultResponse
     */
    @RequestMapping(path = "/findProductList", method = RequestMethod.GET)
    public ResultResponse findProductList() {
        ResultResponse<List<ProductVo>> resultResponse = new ResultResponse<>();
        List<ProductVo> productVoList = productService.findProductVoList();
        if (productVoList != null) {
            resultResponse.selectSuccess(productVoList);
        } else {
            resultResponse.selectFail(ResultResponse.SELECT_FAIL);
        }
        return resultResponse;
    }

    /**
     * 更新单个商品
     * /updateProduct
     *
     * @param product 商品对象
     * @return 返回resultResponse 指定泛型 Integer 成功返回更新所影响的行数,失败则返回错误信息
     * @see ResultResponse
     */
    @RequestMapping(path = "/updateProduct", method = RequestMethod.POST)
    public ResultResponse updateProduct(@RequestBody Product product) {
        int i = productService.updateProductById(product);
        ResultResponse<Integer> resultResponse = new ResultResponse<>();
        if (i > 0) {
            resultResponse.updateSuccess(i);
        } else {
            resultResponse.updateFail(ResultResponse.UPDATE_FAIL);
        }
        return resultResponse;
    }

    /**
     * 删除单个商品
     * 删除时连同商品的具体属性一起删除
     * /deleteProduct/{id}
     *
     * @param id 商品id
     * @return 返回resultResponse 指定泛型 Integer 成功返回删除所影响的行数,失败则返回错误信息
     * @see ResultResponse
     */
    @RequestMapping(path = "/deleteProduct/{id}", method = RequestMethod.DELETE)
    public ResultResponse deleteProduct(@PathVariable("id") Integer id) {
        ResultResponse<Integer> resultResponse = new ResultResponse<>();
        int i = productService.deleteProductById(id);
        if (i > 0) {
            resultResponse.deleteSuccess(i);
        } else {
            resultResponse.deleteFail(ResultResponse.DELETE_FAIL);
        }
        return resultResponse;
    }

    /**
     * 添加单个商品
     * 添加时判断sku和uuid是否有相同的，否则不给添加
     * /insertProduct
     *
     * @param product 商品对象
     * @return 返回resultResponse 指定泛型 Integer 成功返回添加所影响的行数,失败则返回错误信息
     * @see ResultResponse
     */
    @RequestMapping(path = "/insertProduct", method = RequestMethod.PUT)
    public ResultResponse insertProduct(@RequestBody Product product) {
        ResultResponse<Product> resultResponse = new ResultResponse<>();
        if (product.getProductCreateTime() != null) {
            long time = product.getProductCreateTime().getTime();
            if (time > System.currentTimeMillis()) {
                String msg = "生产日期错误";
                resultResponse.insertFail(msg);
                return resultResponse;
            }
        }
        product.setStatus(1);
        Product productInsert = productService.insertProduct(product);
        if (productInsert != null) {
            resultResponse.insertSuccess(productInsert);
        } else {
            String msg = "可能有相同的sku或者uuid,插入失败";
            resultResponse.insertFail(msg);
        }
        return resultResponse;
    }

    /**
     * /findProductListByPage/{page}/{size}
     * 分页查询数据,页码和大小可定义
     *
     * @param page 页码
     * @param size 每页的大小
     * @return 返回resultResponse 指定泛型Page<ProductVo> 返回mybatis-plus封装的分页对象
     * @see com.baomidou.mybatisplus.extension.plugins.pagination.Page
     * @see ResultResponse
     * protected List<T> records;
     * protected long total;
     * protected long size;
     * protected long current;
     * ......
     */
    @RequestMapping(path = "/findProductListByPage/{page}/{size}", method = RequestMethod.GET)
    public ResultResponse getProductListByPage(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        Page<ProductVo> productByPage = productService.findProductByPage(page, size);
        ResultResponse<Page<ProductVo>> resultResponse = new ResultResponse<>();
        if (Objects.nonNull(productByPage)) {
            resultResponse.selectSuccess(productByPage);
            return resultResponse;
        } else {
            resultResponse.selectFail(ResultResponse.SELECT_FAIL);
            return resultResponse;
        }
    }
}
